﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace DynamicObject_t.Options
{  

    
        public static class Log
        {
            private static bool allowed = bool.Parse(Properties.Settings.Default.LogUsing);
            private static string defaultPath = Properties.Settings.Default.LogDirectory;
            private static string prefix = Properties.Settings.Default.LogFilePrefix;

            public static string Prefix
            {
                set { Log.prefix = value; }
            }


            private static string GetFileName(DateTime date)
            {
                return defaultPath + prefix + date.ToString("yyyy-MM-dd") + "-log.txt";
            }

            private static StreamWriter GetStream()
            {
                StreamWriter sw = null;
                string fileName = GetFileName(DateTime.Now);
                if (!Directory.Exists(defaultPath))
                {
                    Directory.CreateDirectory(defaultPath);
                }
                bool exists = File.Exists(fileName);

                sw = exists ? File.AppendText(fileName) : File.CreateText(fileName);
                if (!exists)
                {
                    sw.WriteLine(GetTimeStr() + " " + "Log created");
                }
                return sw;
            }

            private static string GetTimeStr()
            {
                return ((DateTime.Now.Hour < 10) ? "0" : "") + DateTime.Now.Hour.ToString() + ":" + ((DateTime.Now.Minute < 10) ? "0" : "") + DateTime.Now.Minute.ToString() + ":" + ((DateTime.Now.Second < 10) ? "0" : "") + DateTime.Now.Second.ToString() + ": ";
            }

            public static void WriteLine(string text)
            {
                if (allowed)
                {
                    try
                    {
                        using (StreamWriter sw = GetStream())
                        {
                            sw.WriteLine(GetTimeStr() + " " + text);
                            sw.Close();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            public static void WriteErrorLine(string text)
            {
                if (allowed)
                {
                    try
                    {
                        using (StreamWriter sw = GetStream())
                        {
                            sw.WriteLine(GetTimeStr() + " " + text);
                            sw.Close();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            public static void WriteBuilterLine(byte[] text)
            {
                if (allowed)
                {
                    try
                    {
                        using (StreamWriter sw = GetStream())
                        {

                            //for (int i = 0; i < text.Length; i++)
                            //{
                            sw.WriteLine(Encoding.ASCII.GetString(text));
                            sw.Flush();
                            //}

                            //sw.WriteLine(GetTimeStr() + " " + text);
                            //sw.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteLine("Exception error--- " + ex.Message + " " + ex.StackTrace);
                    }
                }
            }


        }
    

}
